package com.lyl.servicesms.service.impl;


import com.lyl.servicesms.service.MsmService;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class MsmServiceImpl implements MsmService {

    @Autowired
     RedisTemplate<String, String> redisTemplate;



    @Autowired
    ZhenziSmsClient client;

    /**
     * 阿里云发送短信
     */
    public boolean send(String PhoneNumbers, String templateCode, Map<String,Object> param) {

        if(StringUtils.isEmpty(PhoneNumbers)) return false;
//
//        DefaultProfile profile =
//                DefaultProfile.getProfile(SmsConfig.REFIONI_D, SmsConfig.ACCESS_KEY_ID, SmsConfig.ACCESS_KEY_SECRET);
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        CommonRequest request = new CommonRequest();
//        //request.setProtocol(ProtocolType.HTTPS);
//        request.setMethod(MethodType.POST);
//        request.setDomain("dysmsapi.aliyuncs.com");
//        request.setVersion("2017-05-25");
//        request.setAction("SendSms");
//
//        request.putQueryParameter("PhoneNumbers", PhoneNumbers);
//        request.putQueryParameter("SignName", "阿里云短信测试");
//        request.putQueryParameter("TemplateCode", templateCode);
//        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
//
//        try {
//            CommonResponse response = client.getCommonResponse(request);
//            System.out.println(response.getData());
//            return response.getHttpResponse().isSuccess();
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
        return false;
    }

    /**
     * 榛子云发送短信
     * @param phoneNumbers
     * @return
     */
    @Override
    public boolean send(String phoneNumbers) {
        // 电话号码为空
        if(StringUtils.isEmpty(phoneNumbers)) return false;
        // redis中的验证码还没有过期
        String code = redisTemplate.opsForValue().get(phoneNumbers);
        if(!StringUtils.isEmpty(code)) return true;

        Random dr= new Random();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("number", phoneNumbers);
        params.put("templateId", "7503");
        String[] templateParams = new String[2];
        templateParams[0] = (dr.nextInt(899999)+100000)+"";
        System.out.println("验证码:" + templateParams[0]);
        templateParams[1] = "10分钟";
        params.put("templateParams", templateParams);
        try {
            String result = client.send(params);
            // 存到redis
            redisTemplate.opsForValue().set(phoneNumbers, templateParams[0],10, TimeUnit.MINUTES);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
